﻿using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;

namespace JSLCloud.Utilities.BarCode
{
    /// <summary>
    /// BarCode Helper
    /// </summary>
    public class BarCodeHelper
    {
        /// <summary>
        /// 二维码生成
        /// </summary>
        /// <param name="Code">编码内容</param>
        /// <returns></returns>
        public static Bitmap Create(string Code)
        {
            try
            {
                var qrCodeGenerator = new QRCodeGenerator();
                var qrCodeData = qrCodeGenerator.CreateQrCode(Code, QRCodeGenerator.ECCLevel.H);
                var qrCode = new QRCode(qrCodeData);
                return qrCode.GetGraphic(15,Color.Black,Color.White,true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 二维码生成
        /// 默认CODE128编码
        /// 保存物理文件
        /// </summary>
        /// <param name="Code"></param>
        /// <param name="Path"></param>
        public static void Create(string Code,string Path)
        {
            try
            {
                var qrCodeGenerator = new QRCodeGenerator();
                var qrCodeData = qrCodeGenerator.CreateQrCode(Code, QRCodeGenerator.ECCLevel.L);
                var qrCode = new QRCode(qrCodeData);
                var bitmap = qrCode.GetGraphic(10, Color.Black, Color.White, true);
                bitmap.Save(Path, ImageFormat.Jpeg);
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// 创建QRCode
        /// </summary>
        /// <param name="content"></param>
        /// <param name="level"></param>
        /// <param name="pixelSize"></param>
        /// <returns></returns>
        public static Bitmap Create(string content, QRCodeGenerator.ECCLevel level,int pixelSize)
        {
            var codeGen = new QRCodeGenerator();
            var codeData = codeGen.CreateQrCode(content, level);
            var code = new QRCode(codeData);
            var bmp = code.GetGraphic(pixelSize);
            return bmp;
        }

    }
}
